from datetime import datetime,timedelta

from jwt import ExpiredSignatureError, InvalidTokenError

from keepback.settings import SECRET_KEY

import jwt

#python jwt 时间日期
import jwt
from datetime import datetime,timedelta
from django.conf import settings

class JwtTTool:
    def get_token(self,userid):
        now = datetime.utcnow()
        exp = now + timedelta(days=1)
        payload = {
            'userid': userid,
            'iat': now,
            'exp': exp
        }
        key = settings.SECRET_KEY
        return "Bearer "+jwt.encode(payload, key, algorithm='HS256')

    # 解析token
    def checktoken(self,token):
        if token.startswith("Bearer "):
            token = token[7:]
        return jwt.decode(token,settings.SECRET_KEY,algorithms=["HS256"])

# 拿到类得实例
jwtTool = JwtTTool()
# print(tool.get_token(1))

